<?php

/**
 * Shopping cart contents page
 *
 * Shopalong, the open source shopping system
 * http://shopalong.blogspot.com/
 *
 * @author Stuart Metcalfe <shoopdev@gmail.com>
 * @copyright Copyright &copy; 2006, Shopalong
 * @copyright Portions Copyright &copy; 2003 osCommerce
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

require('includes/application_top.php');

/**
 * Process shopping cart actions
 *
 * @todo Need to get the goto address from the navigation object and set a
 * snapshot to return to so we don't have to keep on passing non-relevant GET
 * parameters in order to retain a working "Continue" button.
 */

$action = (isset($HTTP_GET_VARS['action'])) ? $HTTP_GET_VARS['action'] : '';

// redirect to a friendly 'enable cookies' page if cookies are disabled
if (!$session->isStarted())
{
	tep_redirect(tep_href_link(FILENAME_COOKIE_USAGE));
}

/**
 * @todo This block needs to be moved either to the following switch statement
 * for each case or after the switch statement.  It makes no sense being at this
 * point in the script.
 */

if (isset($HTTP_GET_VARS['action']))
{
	if (DISPLAY_CART == 'true')
	{
		$goto =  FILENAME_SHOPPING_CART;
		$parameters = array('action', 'cPath', 'products_id', 'pid');
	}
	
	else
	{
		$goto = basename($PHP_SELF);
		
		if ($HTTP_GET_VARS['action'] == 'buy_now')
		{
			$parameters = array('action', 'pid', 'products_id');
		}
		
		else
		{
			$parameters = array('action', 'pid');
		}
	}
}

/**
 * Possible cart actions are:
 * - update Process bulk delete/update quantity request
 * - add Add product
 * - buy_now: Performed by the 'buy now' button in product listings and review
 * page
 * - cust_order @todo Describe this action
 */

switch ($HTTP_GET_VARS['action'])
{
case 'update':
	$cartDeletions = $HTTP_POST_VARS['cart_delete'];
	
	for ($i=0, $n=sizeof($HTTP_POST_VARS['products_id']); $i<$n; $i++)
	{
		$productId = $HTTP_POST_VARS['products_id'][$i];
		$quantity = $HTTP_POST_VARS['cart_quantity'][$i];
		
		if (!is_array($cartDeletions))
		{
			$cartDeletions = array();
		}
		
		if (in_array($productId, $cartDeletions))
		{
			$cart->removeProduct($productId);
		}
		
		else
		{
			if (PHP_VERSION < 4)
			{
				// if PHP3, correct for lack of multidimensional array.
				reset($HTTP_POST_VARS);
				
				while (list($key, $value) = each($HTTP_POST_VARS))
				{
					if (is_array($value))
					{
						while (list($key2, $value2) = each($value))
						{
							if (ereg ("(.*)\]\[(.*)", $key2, $var))
							{
								$id2[$var[1]][$var[2]] = $value2;
							}
						}
					}
				}
				
				$attributes = ($id2[$productId]) ? $id2[$productId] : '';
			}
			
			else
			{
				$id = $HTTP_POST_VARS['id'];
				$attributes = ($id[$productId]) ? $id[$productId] : '';
			}
			
			$cart->addProduct($productId, $quantity, $attributes, false);
		}
	}
	
	tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters)));
	break;
	
case 'add':
	if (isset($HTTP_POST_VARS['products_id']) && is_numeric($HTTP_POST_VARS['products_id']))
	{
		$id = $HTTP_POST_VARS['id'];
		$productId = $HTTP_POST_VARS['products_id'];
		$whichId = tep_get_uprid($productId, $id);
		$modQuantity = 1;
		
		if (isset($HTTP_POST_VARS['quantity']) && is_numeric($HTTP_POST_VARS['quantity']))
		{
			$modQuantity = $HTTP_POST_VARS['quantity'];
		}
		
		$quantity = $cart->getProductQuantity($whichId)+$modQuantity;
		
		if (PHP_VERSION < 4)
		{
			// if PHP3, correct for lack of multidimensional array.
			reset($HTTP_POST_VARS);
			
			while (list($key, $value) = each($HTTP_POST_VARS))
			{
				if (is_array($value))
				{
					while (list($key2, $value2) = each($value))
					{
						if (ereg ("(.*)\]\[(.*)", $key2, $var))
						{
							$id2[$var[1]][$var[2]] = $value2;
						}
					}
				}
			}
			
			$attributes = ($id2[$productId]) ? $id2[$productId] : '';
		}
		
		else
		{
			$id = $HTTP_POST_VARS['id'];
			$attributes = ($id[$productId]) ? $id[$productId] : '';
		}
		
		$cart->addProduct($productId, $quantity, $attributes, false);
	}
	
	tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters)));
	break;
	
case 'buy_now':
	if (isset($HTTP_GET_VARS['products_id']))
	{
		$productId = $HTTP_POST_VARS['products_id'];
		
		if (tep_has_product_attributes($productId))
		{
			$query = 'products_id='.$productId;
			tep_redirect(tep_href_link(FILENAME_PRODUCT_INFO, $query));
		}
		
		else
		{
			$quantity = $cart->getProductQuantity($productId)+1;
			$cart->addProduct($productId, $quantity);
		}
	}
	
	tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters)));
	break;
	
case 'cust_order':
	if ($session->isRegistered('customer_id') && isset($HTTP_GET_VARS['pid']))
	{
		$pid = $HTTP_GET_VARS['pid'];
		
		if (tep_has_product_attributes($pid))
		{
			$query = 'products_id='.$pid;
			tep_redirect(tep_href_link(FILENAME_PRODUCT_INFO, $query));
		}
		
		else
		{
			$quantity = $cart->getProductQuantity($pid);
			$cart->addProduct($pid, $quantity+1);
		}
	}
	
	tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters)));
	break;
default: // Do nothing
}

$theme->display('shopping_cart.tpl');
require(DIR_WS_INCLUDES . 'application_bottom.php');

?>
